# coding=utf-8
# author='kungege21'
# date=2023/8/7 13:42
from typing import Optional

import uvicorn
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()     # app名字随意


# Pydantic 实例类
class CityInfo(BaseModel):
    provice: str
    country: str
    is_affected: Optional[bool] = None  # 可以不传，默认是null


@app.get('/')
async def hello_world():    # async 为异步
    return {'hello': 'world'}


@app.get('/city/{city}')    # {city}路径参数    必填，不能为空
def result(city: str, query_string: Optional[str] = None):      # query_string 为查询参数；可选参数None
    return {'city': city, 'query_string': query_string}


@app.get('/query')
def result(query_string: str = "default"):      # 设置默认参数；无默认值时为必填
    return {'query_string': query_string}


@app.put('/city/{city}')
async def result(city: str, city_info: CityInfo):
    return {'city': city, "country": city_info.country, 'is_affected': city_info.is_affected}

# 启动命令 uvicorn hello_world:app --reload
#   127.0.0.1:8000/docs     127.0.0.1:8000/redoc
if __name__ == '__main__':
    uvicorn.run('hello_world:app',
                host='0.0.0.0',
                port=8000,
                reload=True,
                )
